package com.example.booktest1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;

public class KeyTest extends Activity implements OnKeyListener {
	StringBuilder builder = new StringBuilder();
	TextView textView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textView = new TextView(this);
		textView.setText("Press keys (if you have some)");
		textView.setOnKeyListener(this);
		textView.setFocusableInTouchMode(true);
		textView.requestFocus();
		setContentView(textView);
	}

	@Override
	public boolean onKey(View v, int keyCode,  KeyEvent event) {

		builder.setLength(0);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN :
			builder.append("down, ");
			break;
		case MotionEvent.ACTION_UP :
			builder.append("up, ");
			break;
		}

		builder.append(event.getKeyCode());
		builder.append(", ");
		builder.append((char) event.getUnicodeChar());
		String text = builder.toString();
		Log.d("Key Test", text);
		textView.setText(text);

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
			return false;
		} else {
			return true;
		}

	}

}
